
import { createRouter, createWebHistory } from 'vue-router'
import Admin from "~/layouts/admin.vue"
import Index from '~/pages/index.vue'
import Login from "~/pages/login.vue"
import NotFound from "~/pages/404.vue"
import GoodsList from "~/layouts/goods/list.vue"
import CategoryList from "~/layouts/category/list.vue" 
import CommentList from "~/layouts/comment/list.vue"
import CouponList from "~/layouts/coupon/list.vue"
import imageList from "~/layouts/image/list.vue"
import noticeList from "~/layouts/notice/list.vue"
import orderList from "~/layouts/order/list.vue"
import settingBase from "~/layouts/setting/base.vue"
import userList from "~/layouts/user/list.vue"
import managerList from "~/layouts/manager/list.vue"
import HuiYuan from "~/layouts/level/list.vue"
import accessList from "~/layouts/access/list.vue"
const routes = [
    {
        path: "/",
        name: "admin",
        component: Admin,

    },
    {
        path: "/login",
        component: Login,
        meta: {
            title: "登录"
        }
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }]
// 动态路由,用来匹配菜单的动态路由
const asyncRoutes = [{
    path: "/",
    name: '/',
    component: Index,
    meta: {
        title: "后台首页"
    }
}, {
    path: "/goods/list",
    name: '/goods/list',
    component: GoodsList,
    meta: {
        title: "商品列表"
    }
}, {
    path: "/category/list",
    name: '/category/list',
    component:CategoryList,
    meta: {
        title: "分类列表"
    }
},
{
    path: "/comment/list",
    name: '/comment/list',
    component:CommentList,
    meta: {
        title: "评价列表"
    }
},
{
    path: "/coupon/list",
    name: '/coupon/list',
    component:CouponList,
    meta: {
        title: "优惠券列表"
    }
},
{
    path: "/image/list",
    name: '/image/list',
    component:imageList,
    meta: {
        title: "图库列表"
    }
},
{
    path: "/notice/list",
    name: '/notice/list',
    component:noticeList,
    meta: {
        title: "公告列表"
    }
},
{
    path: "/order/list",
    name: '/order/list',
    component:orderList,
    meta: {
        title: "订单列表"
    }
},
{
    path: "/setting/base",
    name: '/setting/base',
    component:settingBase,
    meta: {
        title: "配置列表"
    }
},
{
    path: "/user/list",
    name: '/user/list',
    component:userList,
    meta: {
        title: "用户列表"
    }
},
{
    path: "/manager/list",
    name: '/manager/list',
    component:managerList,
    meta: {
        title: "管理员列表"
    }
},

{
    path: "/level/list",
    name: '/level/list',
    component:HuiYuan,
    meta: {
        title: "会员列表"
    }
},
{
    path: "/access/list",
    name: '/access/list',
    component:accessList,
    meta: {
        title: "权限列表"
    }
}
]  



export const router = createRouter({
    history: createWebHistory(),
    routes
})

export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false
    const findAndAddRouterByMenus = (arr) => {
        arr.forEach(e => {
            let item = asyncRoutes.find(o => o.path === e.frontpath)
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("admin", item)
                hasNewRoutes = true
            }
            // 添加子一级菜单动态路由
            if (e.child && e.child.length > 0) {
                findAndAddRouterByMenus(e.child)
            }
        });
    }
 
    findAndAddRouterByMenus(menus)
    return hasNewRoutes
}